<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
	@ $KeepLog = $_POST['KeepLog'];
	@ $GetEmail = $_POST['GetEmail'];
	if ( $KeepLog == 1 )
	{
		if (isset($_COOKIE["studial"]))
		{
		}
		else
		{
			$url = "/";
			$expire = time() + 60*60*24*7;
			setcookie("studial", $GetEmail, $expire, $url);
		}
	}
	else
	{
		if (isset($_COOKIE["studial"]))
		{
		}
		else
		{
			$url = "/";
			$expire = time() + 60*30;
			setcookie("studial", $GetEmail, $expire, $url);
		}
	}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Check_login</title>
    	<script language="javascript" type="text/javascript">
            function Highlight_Box_On(current)
            {	
                document.getElementById(current).style.backgroundColor = "#00EEFF";
            }
            function Highlight_Box_Off(current)
            {
                document.getElementById(current).style.backgroundColor = "white";
            }
			function CreateAcc()
			{
				window.location="../register/form.php";
			}
        </script>
</head>

<body>
	<?php
			@ $Email = $_COOKIE['studial'];
		
            @ $User = $_POST['User'];
            @ $Pass = $_POST['Pass'];
			
            $User = trim($User);
            $Pass = trim($Pass);
			
            if (!$User || !$Pass)
            {
                echo '<h2 align="center" style="color:#000000">Bạn phải điền đầy đủ tên đăng nhập và mật khẩu!</h2>';
		?>
        
        <form action="check_login.php" method="post">
            <table bordercolor="#0000CC" style="margin-top:5%; margin-left:55%;">
            	<tr>
                	<td align="center"><b>SIGN INTO STUDIAL</b></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td align="center">Don't have a Studial account?</td>
                </tr>
                <tr>
                	<td align="center"><a href="../register/form.php"><input type="button" value="Create New Account" style="background-color:#FFFF00" /></a></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                    <td>Email:</td>
                </tr>
                <tr>
                    <td><input type="text" name="User" id="User" onfocus="Highlight_Box_On('User')" onblur="Highlight_Box_Off('User')" maxlength="15" /></td>
                </tr>
                <tr>
                    <td>(e.g. itec@itec.hcmus.edu.vn)</td>
                </tr>
                <tr>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="password" name="Pass" id="Pass" onfocus="Highlight_Box_On('Pass')" onblur="Highlight_Box_Off('Pass')" maxlength="100" /></td>
                </tr>
                <tr align="center">
                    <td colspan="2"><input type="submit" value="Sign In" style="background-color:#CCCCCC" /></td>
                </tr>
                <tr>
                	<td><input type="checkbox" name="KeepLog" value="1" checked="checked" />Keep me signed in</td>
                </tr>
                <tr>
                	<td>(Uncheck if on a shared computer)</td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td><div align="center"><a href="forget_pw.html">I cannot access my account</a></div></td>
                </tr>
            </table>
        </form>
        
        <?php
                exit;
            }
            $User = addslashes($User);
            $Pass = addslashes($Pass);
			$Pass = hash("sha512", $Pass);
			
			$ini_array = parse_ini_file("../config.ini");
			@ $db = mysql_pconnect($ini_array['dblink'], $ini_array['dbuser'], $ini_array['dbpass']);
                    
            @ $db = mysql_pconnect("localhost","root","");
            if(!$db)
            {
                echo "<h2>Database does not exist</h2><br/>";
                echo "<p>Error: ".mysql_error()."</p>";
                exit;
            }
			
            @ mysql_set_charset('utf8');
			@ mysql_select_db($ini_array['dbname']);
			
            $Select = "SELECT * FROM `users` WHERE `users`.`primaryemail` like '".$User."' and `users`.`password` like '".$Pass."'";
			$query = "INSERT INTO 1059020_cookies (primaryemail, Cookie) "."VALUES ('$User','$Email')";
			
            $result = mysql_query($Select);
			$resultCookie = mysql_query($query);
            
            $NumOfRecord = mysql_num_rows($result);
            $row = mysql_fetch_array($result);
    
            $primaryemail = stripslashes($row['primaryemail']);
            $password = stripslashes($row['password']);
			$type = stripslashes($row['Type']);
            
            if( ($primaryemail != false) || ($password != false) )
            {
                header('location: ../personal/home.php');
                exit;
            }
            else
            {
                echo '<h2 align="center" style="color:#CC0000">Bạn đã nhập sai tên đăng nhập hay mật khẩu!</h2>';
		?>
        
        <form action="check_login.php" method="post">
            <table bordercolor="#0000CC" style="margin-top:5%; margin-left:55%;">
            	<tr>
                	<td align="center"><b>SIGN INTO STUDIAL</b></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td align="center">Don't have a Studial account?</td>
                </tr>
                <tr>
                	<td align="center"><a href="../register/form.php"><input type="button" value="Create New Account" style="background-color:#FFFF00" /></a></td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                    <td>Email:</td>
                </tr>
                <tr>
                    <td><input type="text" name="User" id="User" onfocus="Highlight_Box_On('User')" onblur="Highlight_Box_Off('User')" maxlength="15" /></td>
                </tr>
                <tr>
                    <td>(e.g. itec@itec.hcmus.edu.vn)</td>
                </tr>
                <tr>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="password" name="Pass" id="Pass" onfocus="Highlight_Box_On('Pass')" onblur="Highlight_Box_Off('Pass')" maxlength="100" /></td>
                </tr>
                <tr align="center">
                    <td colspan="2"><input type="submit" value="Sign In" style="background-color:#CCCCCC" /></td>
                </tr>
                <tr>
                	<td><input type="checkbox" name="KeepLog" value="1" checked="checked" />Keep me signed in</td>
                </tr>
                <tr>
                	<td>(Uncheck if on a shared computer)</td>
                </tr>
                <tr>
                	<td><hr /></td>
                </tr>
                <tr>
                	<td><div align="center"><a href="forget_pw.html">I cannot access my account</a></div></td>
                </tr>
            </table>
        </form>
        
        <?php
				exit;
            }
        ?>
</body>
</html>